package org.dzl.springboot001.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dzl.springboot001.common.Tools;
import org.dzl.springboot001.config.JSONResult;
import org.dzl.springboot001.config.MyBean;
import org.dzl.springboot001.entity.Cat;
import org.dzl.springboot001.entity.MyUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 光的代言人
 */
@Controller
@RequestMapping("test")
public class TestController {
    @Autowired
    private Tools tools;

    @RequestMapping("t1")
    public String t1() {
        return "test/t1";
    }

    @RequestMapping("t2")
    @ResponseBody
    public String t2(String name) {
        System.out.println(name.equals("哈哈哈哈"));
        return "test/t2";
    }

    @RequestMapping("t3")
    @ResponseBody
    public Map t3(Integer a, Integer b) {
        System.out.println(tools.add(a,b));
        Map<String,Object> map=new HashMap<>();
        map.put("name","");
        map.put("age",18);
//        map.put("sum",tools.add(a,b));
        return map;
    }
    // 手动转换成 json 字符串
    @GetMapping("t4")
    @ResponseBody
    public String t4() throws JsonProcessingException {
        Cat cat = new Cat("小猫", 3, 1);
        ObjectMapper objectMapper = new ObjectMapper();
        String json=objectMapper.writeValueAsString(cat);
        return json;
    }

//    @GetMapping("t5")
//    @ResponseBody
//    public Map<String,Object> t5(){
//        Map<String,Object> map=new HashMap<>();
//        map.put("code","1");
//        map.put("msg","成功");
//        map.put("data",new Cat("小猫",3,1));
//        return map;
//    }

    @GetMapping("t5")
    @ResponseBody
    public JSONResult<Cat> t5(){

        return new JSONResult<Cat>("success",0,new Cat("小猫",3,1));
    }

    @Autowired
    private MyUrl myUrl;

    @GetMapping("t6")
    @ResponseBody
    public String t6(){
        System.out.println(myUrl.getOrderUrl());
        System.out.println(myUrl.getUserUrl());
        System.out.println(myUrl.getShoppingUrl());
        return "success";
    }
}
